package com.marketing.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 网关健康检查和监控控制器
 * 
 * @author Marketing Team
 * @version 1.0.0
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/gateway")
public class GatewayHealthController implements HealthIndicator {

    private static final Logger log = LoggerFactory.getLogger(GatewayHealthController.class);

    @Autowired
    private RouteLocator routeLocator;

    /**
     * 网关基础信息
     */
    @GetMapping("/info")
    public Mono<Map<String, Object>> getGatewayInfo() {
        log.info("获取网关基础信息");
        
        Map<String, Object> info = new HashMap<>();
        info.put("serviceName", "gateway-service");
        info.put("version", "1.0.0");
        info.put("status", "running");
        info.put("timestamp", LocalDateTime.now());
        info.put("description", "智能营销系统API网关服务");
        
        return Mono.just(info);
    }

    /**
     * 路由信息
     */
    @GetMapping("/routes")
    public Mono<Map<String, Object>> getRoutes() {
        log.info("获取网关路由信息");
        
        return routeLocator.getRoutes()
                .collectList()
                .map(routes -> {
                    Map<String, Object> result = new HashMap<>();
                    result.put("totalRoutes", routes.size());
                    result.put("routes", routes);
                    result.put("timestamp", LocalDateTime.now());
                    return result;
                });
    }

    /**
     * 网关状态检查
     */
    @GetMapping("/status")
    public Mono<Map<String, Object>> getGatewayStatus() {
        log.info("检查网关状态");
        
        Map<String, Object> status = new HashMap<>();
        status.put("gateway", "healthy");
        status.put("timestamp", LocalDateTime.now());
        status.put("uptime", System.currentTimeMillis());
        
        return Mono.just(status);
    }

    /**
     * Spring Boot Actuator 健康检查
     */
    @Override
    public Health health() {
        try {
            // 这里可以添加更复杂的健康检查逻辑
            // 比如检查依赖服务是否可用等
            
            return Health.up()
                    .withDetail("gateway", "running")
                    .withDetail("timestamp", LocalDateTime.now())
                    .withDetail("status", "healthy")
                    .build();
        } catch (Exception e) {
            log.error("网关健康检查失败", e);
            return Health.down()
                    .withDetail("error", e.getMessage())
                    .withDetail("timestamp", LocalDateTime.now())
                    .build();
        }
    }
}